package com.sanshi.project.assets.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanshi.project.admin.api.entity.InspectItemTemplateEntity;
import com.sanshi.project.assets.mapper.InspectItemTemplateMapper;
import com.sanshi.project.assets.service.InspectItemTemplateService;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sanshi.project.admin.api.entity.InspectItemEntity;
import com.sanshi.project.assets.mapper.InspectItemMapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import java.util.Objects;
/**
 * 巡检项模版
 *
 * @author nikon
 * @date 2024-01-01 15:47:14
 */
@Service
@RequiredArgsConstructor
public class InspectItemTemplateServiceImpl extends ServiceImpl<InspectItemTemplateMapper, InspectItemTemplateEntity> implements InspectItemTemplateService {
  private final InspectItemMapper inspectItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDeep(InspectItemTemplateEntity inspectItemTemplate) {
        baseMapper.insert(inspectItemTemplate);
        for (InspectItemEntity  inspectItem : inspectItemTemplate.getInspectItemList()) {
            inspectItem.setRelateId(inspectItemTemplate.getId());
            inspectItemMapper.insert( inspectItem);
        }

        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDeep(InspectItemTemplateEntity inspectItemTemplate) {
        baseMapper.updateById(inspectItemTemplate);
        for (InspectItemEntity  inspectItem : inspectItemTemplate.getInspectItemList()) {
            if (Objects.isNull(inspectItem.getId())) {
                inspectItem.setRelateId(inspectItemTemplate.getId());
                inspectItemMapper.insert(inspectItem);
            } else {
                inspectItemMapper.updateById(inspectItem);
            }
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDeep(Long[] ids) {
        baseMapper.deleteBatchIds(CollUtil.toList(ids));
        inspectItemMapper.delete(Wrappers.<InspectItemEntity>lambdaQuery().in(InspectItemEntity::getRelateId, ids));
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeChild(Long[] ids) {
        inspectItemMapper.deleteBatchIds(CollUtil.toList(ids));
        return Boolean.TRUE;
    }
}